package com.health.healthproject.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.healthproject.common.Result;
import com.health.healthproject.dto.ServiceRecordDTO;
import com.health.healthproject.entity.ServiceRecord;
import com.health.healthproject.entity.User;
import com.health.healthproject.mapper.UserMapper;
import com.health.healthproject.service.ServiceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 服务记录管理控制器
 */
@RestController
@RequestMapping("/service-records")
public class ServiceRecordController {

    @Autowired
    private ServiceRecordService serviceRecordService;
    @Qualifier("userMapper")
    @Autowired
    private UserMapper userMapper;

    /**
     * 新增服务记录
     *
     * @param serviceRecord 服务记录信息
     * @return 添加结果，true表示成功，false表示失败
     */
    //@SaCheckRole("2")
    @PostMapping
    public Result<Boolean> add(@RequestBody ServiceRecord serviceRecord) {
        List<String> roleList = StpUtil.getRoleList();
        System.out.println(roleList);
        StpUtil.checkRole("2");
        boolean success = serviceRecordService.createServiceRecord(serviceRecord);
        return Result.success(success);
    }

    /**
     * 删除服务记录
     *
     * @param id 服务记录ID
     * @return 删除结果，true表示成功，false表示失败
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = serviceRecordService.deleteServiceRecord(id);
        return Result.success(success);
    }

    /**
     * 更新服务记录
     *
     * @param serviceRecord 服务记录信息
     * @return 更新结果，true表示成功，false表示失败
     */
    @PutMapping("/update")
    public Result<Boolean> update(@RequestBody ServiceRecord serviceRecord) {
        boolean success = serviceRecordService.updateServiceRecord(serviceRecord);
        return Result.success(success);
    }

    /**
     * 根据ID查询服务记录详情
     *
     * @param id 服务记录ID
     * @return 服务记录详情，包含关联用户的姓名信息
     */
    @GetMapping("/{id}")
    public Result<ServiceRecordDTO> getById(@PathVariable Long id) {
        ServiceRecordDTO serviceRecordDTO = serviceRecordService.getServiceRecordWithUserNames(id);
        return Result.success(serviceRecordDTO);
    }

    /**
     * 分页查询服务记录列表
     *
     * @param pageNum  当前页码，默认1
     * @param pageSize 每页数量，默认10
     * @param query    查询条件，可包含用户ID、服务人员ID、服务日期等
     * @return 分页数据，包含服务记录列表及关联用户的姓名信息
     */
    @GetMapping("/page")
    public Result<Page<ServiceRecordDTO>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String startDates,
            @RequestParam(defaultValue = "") String endDate,
            ServiceRecord query) {
        //IPage<ServiceRecordDTO> result = serviceRecordService.pageServiceRecordWithUserNames(pageNum, pageSize, query);
        Page<ServiceRecordDTO> page = new Page<>(pageNum, pageSize);
        User user = userMapper.selectById(StpUtil.getLoginIdAsLong());
        if (StringUtils.isBlank(user.getUserAuth())){
            query.setUserId(user.getId());
        }
        Page<ServiceRecordDTO> result = serviceRecordService.pageServiceRecord(page,query,startDates,endDate);
        return Result.success(result);
    }
}